﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WhiteBoard
{
    public partial class Magnifier : Form
    {
        public static int i=10;
        public static Rectangle fullScreen = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
        public static Bitmap fullBmp;
        public  static Bitmap newBmp;
        public static Bitmap primaryBmp;
        public static Rectangle dstRect1, srcRect1, dstRect2, srcRect2;
        public static Graphics gr1;
        public static Graphics gr2;
        public static Graphics gr3 ;
        public static int frame, caption;
        private bool IsMoving = false;
        private bool IsLeaving = false;
        private bool b1 = false, b2 = false;

        //定义移动相关变量

        bool IsMoving2 = false;
        Point pthisLastCoordinate = new Point(0, 0);
        Point pCursorOffset = new Point(0, 0);
        Point pCursorLastCoordinate = new Point(0, 0);


        private static System.Timers.Timer t;
               

        //[DllImport("AutoITx3.DLL", EntryPoint = "AU3_PixelChecksum")]
        //public static extern int PixelChecksum(int x1, int y1, int x2, int y2);
        //public static int pixelSum;

        public Magnifier()
        {
            InitializeComponent();
            Magnifier.frame = (this.Width - this.ClientRectangle.Width) / 2;
            Magnifier.caption = this.Height - this.ClientRectangle.Height - 2 * Magnifier.frame;
            //Rectangle fullScreen = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
            Magnifier.fullBmp = new Bitmap(fullScreen.Width, fullScreen.Height);
            Magnifier.gr1 = Graphics.FromImage(Magnifier.fullBmp);
            Magnifier.gr1.CopyFromScreen(0, 0, 0, 0, fullScreen.Size);
            
            this.Visible = true;
            Magnifier.primaryBmp = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
            Magnifier.newBmp = new Bitmap(Magnifier.primaryBmp.Width, Magnifier.primaryBmp.Height);
            Magnifier.dstRect1 = new Rectangle(0, 0, Magnifier.primaryBmp.Width, Magnifier.primaryBmp.Height);
            Magnifier.srcRect1 = new Rectangle(this.Left + Magnifier.frame, this.Top + Magnifier.caption + Magnifier.frame, this.ClientRectangle.Width, this.ClientRectangle.Height);
            Magnifier.dstRect2 = new Rectangle(0, 0, Magnifier.newBmp.Width, Magnifier.newBmp.Height);
            Magnifier.srcRect2 = new Rectangle(Magnifier.primaryBmp.Width * 1 / 2 - Magnifier.primaryBmp.Width * 4 / i, Magnifier.primaryBmp.Height * 1 / 2 - Magnifier.primaryBmp.Height * 4 / i, Magnifier.primaryBmp.Width - 2 * (Magnifier.primaryBmp.Width * 1 / 2 - Magnifier.primaryBmp.Width * 4 / i), Magnifier.primaryBmp.Height - 2 * (Magnifier.primaryBmp.Height * 1 / 2 - Magnifier.primaryBmp.Height * 4 / i));
            Magnifier.gr2 = Graphics.FromImage(Magnifier.primaryBmp);
            Magnifier.gr2.DrawImage(Magnifier.fullBmp, Magnifier.dstRect1, Magnifier.srcRect1, GraphicsUnit.Pixel);

            Magnifier.gr3 = Graphics.FromImage(Magnifier.newBmp);
            Magnifier.gr3.DrawImage(Magnifier.primaryBmp, Magnifier.dstRect2, Magnifier.srcRect2, GraphicsUnit.Pixel);
            
        }

        private void Magnifier_Load(object sender, EventArgs e)
        {
        //    Magnifier.frame = (this.Width - this.ClientRectangle.Width) / 2;
        //    Magnifier.caption = this.Height - this.ClientRectangle.Height - 2 * Magnifier.frame;
        //    //Rectangle fullScreen = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
        //    //Magnifier.fullBmp = new Bitmap(fullScreen.Width, fullScreen.Height);
        //    //Magnifier.gr1 = Graphics.FromImage(Magnifier.fullBmp);
        //    //Magnifier.gr1.CopyFromScreen(0, 0, 0, 0, fullScreen.Size);
        //    //this.Visible = true;
        //    Magnifier.primaryBmp = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
        //    Magnifier.newBmp = new Bitmap(Magnifier.primaryBmp.Width, Magnifier.primaryBmp.Height);
        //    Magnifier.dstRect1 = new Rectangle(0, 0, Magnifier.primaryBmp.Width, Magnifier.primaryBmp.Height);
        //    Magnifier.srcRect1 = new Rectangle(this.Left + Magnifier.frame, this.Top + Magnifier.caption + Magnifier.frame, this.ClientRectangle.Width, this.ClientRectangle.Height);
        //    Magnifier.dstRect2 = new Rectangle(0, 0, Magnifier.newBmp.Width, Magnifier.newBmp.Height);
        //    Magnifier.srcRect2 = new Rectangle(Magnifier.primaryBmp.Width / 10, Magnifier.primaryBmp.Height / 10, Magnifier.primaryBmp.Width - Magnifier.primaryBmp.Width / 10, Magnifier.primaryBmp.Height - Magnifier.primaryBmp.Height / 10);
        //    Magnifier.gr2 = Graphics.FromImage(Magnifier.primaryBmp);
        //    Magnifier.gr2.DrawImage(Magnifier.fullBmp, Magnifier.dstRect1, Magnifier.srcRect1, GraphicsUnit.Pixel);

        //    Magnifier.gr3 = Graphics.FromImage(Magnifier.newBmp);
        //    Magnifier.gr3.DrawImage(Magnifier.primaryBmp, Magnifier.dstRect2, Magnifier.srcRect2, GraphicsUnit.Pixel);

        //    Graphics gr4 = this.CreateGraphics();
        //    gr4.DrawImage(Magnifier.newBmp, 0, 0);
        //    //gr4.DrawImage(Magnifier.fullBmp,0,0);

        //    Magnifier.gr1.Dispose();
        //    Magnifier.gr2.Dispose();
        //    Magnifier.gr3.Dispose();
        //    gr4.Dispose();
        //    this.IsMoving = true;
        //    //MessageBox.Show("1");
            
        }

        private void Magnifier_Paint(object sender, PaintEventArgs e)
        {
            //Magnifier.frame = (this.Width - this.ClientRectangle.Width) / 2;
            //Magnifier.caption = this.Height - this.ClientRectangle.Height - 2 * Magnifier.frame;
            
            ////Rectangle fullScreen = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
            ////Magnifier.fullBmp = new Bitmap(fullScreen.Width, fullScreen.Height);
            ////Magnifier.gr1 = Graphics.FromImage(Magnifier.fullBmp);
            ////Magnifier.gr1.CopyFromScreen(0, 0, 0, 0, fullScreen.Size);
            ////this.Visible = true;
            ////magnifyForm.Show();
           
            //Magnifier.primaryBmp = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
            //Magnifier.newBmp = new Bitmap(Magnifier.primaryBmp.Width, Magnifier.primaryBmp.Height);
            //Magnifier.dstRect1 = new Rectangle(0, 0, Magnifier.primaryBmp.Width, Magnifier.primaryBmp.Height);
            //Magnifier.srcRect1 = new Rectangle(this.Left + Magnifier.frame, this.Top + Magnifier.caption + Magnifier.frame, this.ClientRectangle.Width, this.ClientRectangle.Height);
            //Magnifier.dstRect2 = new Rectangle(0, 0, Magnifier.newBmp.Width, Magnifier.newBmp.Height);
            //Magnifier.srcRect2 = new Rectangle(Magnifier.primaryBmp.Width / i, Magnifier.primaryBmp.Height / i, Magnifier.primaryBmp.Width - Magnifier.primaryBmp.Width / i, Magnifier.primaryBmp.Height - Magnifier.primaryBmp.Height / i);
            //Magnifier.gr2 = Graphics.FromImage(Magnifier.primaryBmp);
            //Magnifier.gr2.DrawImage(Magnifier.fullBmp, Magnifier.dstRect1, Magnifier.srcRect1, GraphicsUnit.Pixel);

            //Magnifier.gr3 = Graphics.FromImage(Magnifier.newBmp);
            //Magnifier.gr3.DrawImage(Magnifier.primaryBmp, Magnifier.dstRect2, Magnifier.srcRect2, GraphicsUnit.Pixel);

            Graphics gr4 = this.CreateGraphics();
            gr4.DrawImage(Magnifier.newBmp, 0, 0);
            //gr4.DrawImage(Magnifier.fullBmp,0,0);

            //Magnifier.gr1.Dispose();
            //Magnifier.gr2.Dispose();
            //Magnifier.gr3.Dispose();
            gr4.Dispose();
            this.IsMoving = true;
            
            
            
        }

        private void Magnifier_Move(object sender, EventArgs e)
        {
            if (this.IsMoving)
            {

                this.IsMoving = false;
                Magnifier.srcRect1 =new Rectangle(this.Left + frame, this.Top + caption + frame, this.ClientRectangle.Width, this.ClientRectangle.Height);
                Magnifier.gr2.DrawImage(Magnifier.fullBmp, Magnifier.dstRect1, Magnifier.srcRect1, GraphicsUnit.Pixel);
                Magnifier.gr3.DrawImage(Magnifier.primaryBmp, Magnifier.dstRect2, Magnifier.srcRect2, GraphicsUnit.Pixel);
                Graphics gr = this.CreateGraphics();
                gr.DrawImage(Magnifier.newBmp, 0, 0);
                //Magnifier.gr2.Dispose();
                //Magnifier.gr3.Dispose();
                gr.Dispose();
                this.IsMoving = true;
                if (this.Left + frame >= 0 && this.Top + frame + caption >= 0)
                {
                    this.btnDe.Left = this.ClientRectangle.X;
                    this.btnDe.Top = this.ClientRectangle.Y;
                    this.btnEx.Left = this.ClientRectangle.X + this.btnDe.Width;
                    this.btnEx.Top = this.ClientRectangle.Y;
                }
                else if (this.Left + this.Width - frame <= fullScreen.Width && this.Top + frame + caption >= 0)
                {
                    this.btnDe.Left = this.ClientRectangle.X + this.ClientRectangle.Width - 2 * this.btnDe.Width;
                    this.btnDe.Top = this.ClientRectangle.Y;
                    this.btnEx.Left = this.btnDe.Left + this.btnDe.Width;
                    this.btnEx.Top = this.ClientRectangle.Y;
                }
                else if (this.Left + frame >= 0)
                {
                    this.btnDe.Left = this.ClientRectangle.X;
                    this.btnDe.Top = this.ClientRectangle.Y + this.ClientRectangle.Height - this.btnDe.Height;
                    this.btnEx.Left = this.ClientRectangle.X + this.btnDe.Width;
                    this.btnEx.Top = this.btnDe.Top;
                }
                else
                {
                    this.btnDe.Left = this.ClientRectangle.Width - 2 * this.btnDe.Width;
                    this.btnDe.Top = this.ClientRectangle.Height - this.btnDe.Height;
                    this.btnEx.Left = this.ClientRectangle.Width - this.btnEx.Width;
                    this.btnEx.Top = this.btnDe.Top;
                }
            }
        }

        private void Magnifier_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                IsMoving2 = true;
                pthisLastCoordinate.X = this.Left;
                pthisLastCoordinate.Y = this.Top;
                pCursorLastCoordinate.X = Cursor.Position.X;
                pCursorLastCoordinate.Y = Cursor.Position.Y;
            }
        }

        private void Magnifier_SizeChanged(object sender, EventArgs e)
        {
           
                //this.IsResize = false;
            //Magnifier.primaryBmp.Dispose();
            //Magnifier.newBmp.Dispose();
            Magnifier.primaryBmp = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
            Magnifier.newBmp = new Bitmap(Magnifier.primaryBmp.Width, Magnifier.primaryBmp.Height);
            Magnifier.gr2 = Graphics.FromImage(Magnifier.primaryBmp);
            Magnifier.gr3 = Graphics.FromImage(Magnifier.newBmp);
                Magnifier.dstRect1 = new Rectangle(0, 0, Magnifier.primaryBmp.Width, Magnifier.primaryBmp.Height);
                Magnifier.srcRect1 = new Rectangle(this.Left + Magnifier.frame, this.Top + Magnifier.caption + Magnifier.frame, this.ClientRectangle.Width, this.ClientRectangle.Height);
                Magnifier.dstRect2 = new Rectangle(0, 0, Magnifier.newBmp.Width, Magnifier.newBmp.Height);
                Magnifier.srcRect2 = new Rectangle(Magnifier.primaryBmp.Width * 1 / 2 - Magnifier.primaryBmp.Width * 4 / i, Magnifier.primaryBmp.Height * 1 / 2 - Magnifier.primaryBmp.Height * 4 / i, Magnifier.primaryBmp.Width - 2 *( Magnifier.primaryBmp.Width * 1 / 2 -  Magnifier.primaryBmp.Width *4 / i), Magnifier.primaryBmp.Height - 2 *( Magnifier.primaryBmp.Height * 1 / 2 -  Magnifier.primaryBmp.Height *4 / i));
                Magnifier.gr2.DrawImage(Magnifier.fullBmp, Magnifier.dstRect1, Magnifier.srcRect1, GraphicsUnit.Pixel);
                Magnifier.gr3.DrawImage(Magnifier.primaryBmp, Magnifier.dstRect2, Magnifier.srcRect2, GraphicsUnit.Pixel);
                Graphics gr = this.CreateGraphics();
                gr.DrawImage(Magnifier.newBmp, 0, 0);
                gr.Dispose();
                //this.IsResize = true;
            
        }

        private void btnEx_Click(object sender, EventArgs e)
        {
           
                i++;
                Magnifier.srcRect2 = new Rectangle(Magnifier.primaryBmp.Width * 1 / 2 - Magnifier.primaryBmp.Width * 4 / i, Magnifier.primaryBmp.Height * 1 / 2 - Magnifier.primaryBmp.Height * 4 / i, Magnifier.primaryBmp.Width - 2 * (Magnifier.primaryBmp.Width * 1 / 2 - Magnifier.primaryBmp.Width * 4 / i), Magnifier.primaryBmp.Height - 2 * (Magnifier.primaryBmp.Height * 1 / 2 - Magnifier.primaryBmp.Height * 4 / i));
                Magnifier.gr3.DrawImage(Magnifier.primaryBmp, Magnifier.dstRect2, Magnifier.srcRect2, GraphicsUnit.Pixel);
                Graphics gr = this.CreateGraphics();
                gr.DrawImage(Magnifier.newBmp, 0, 0);
                gr.Dispose();
            
        }

        private void btnDe_Click(object sender, EventArgs e)
        {
            if (i >= 9)
            {
                i--;
                Magnifier.srcRect2 = new Rectangle(Magnifier.primaryBmp.Width * 1 / 2 - Magnifier.primaryBmp.Width * 4 / i, Magnifier.primaryBmp.Height * 1 / 2 - Magnifier.primaryBmp.Height * 4 / i, Magnifier.primaryBmp.Width - 2 * (Magnifier.primaryBmp.Width * 1 / 2 - Magnifier.primaryBmp.Width * 4 / i), Magnifier.primaryBmp.Height - 2 * (Magnifier.primaryBmp.Height * 1 / 2 - Magnifier.primaryBmp.Height * 4 / i));
                Magnifier.gr3.DrawImage(Magnifier.primaryBmp, Magnifier.dstRect2, Magnifier.srcRect2, GraphicsUnit.Pixel);
                Graphics gr = this.CreateGraphics();
                gr.DrawImage(Magnifier.newBmp, 0, 0);
                gr.Dispose();
            }
        }

        private void btnDe_Paint(object sender, PaintEventArgs e)
        {
        
        }

      
       

       
        private void Magnifier_Deactivate(object sender, EventArgs e)
        {
            if (b1)
                b2 = true;
            //if (b1)
            //{
            //    b1 = false;
                
            //    this.IsLeaving = true;
            //    while (this.IsLeaving)
            //    {
            //        Bitmap nowBmp = new Bitmap(Magnifier.fullScreen.Width, Magnifier.fullScreen.Height);
            //        Graphics grTmp = Graphics.FromImage(nowBmp);
            //        this.Visible = false;
            //        grTmp.CopyFromScreen(0, 0, 0, 0, fullScreen.Size);
            //        this.b2 = false;
            //        this.Visible = true;
                    
            //        if (!nowBmp.Equals(Magnifier.fullBmp))
            //        {
            //            Magnifier.fullBmp = nowBmp;
            //            Magnifier.gr2.DrawImage(Magnifier.fullBmp, Magnifier.dstRect1, Magnifier.srcRect1, GraphicsUnit.Pixel);
            //            Magnifier.gr3.DrawImage(Magnifier.primaryBmp, Magnifier.dstRect2, Magnifier.srcRect2, GraphicsUnit.Pixel);
            //            Graphics gr = this.CreateGraphics();
            //            gr.DrawImage(Magnifier.newBmp, 0, 0);
            //            gr.Dispose();

            //        }
            //        grTmp.Dispose();
            //    }
            //    b1 = true; 
            //}
        }

        private void Magnifier_Activated(object sender, EventArgs e)
        {

            if (b2)
            {
                //System.Threading.Thread.Sleep(500);
                //for (int i = 0; i != 10000;i++ ) ;
                t = new System.Timers.Timer(500);
                t.AutoReset = false;
                t.Enabled = true;
                t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
                b2 = false;
                b1 = false;
                this.Opacity = 0;
                Magnifier.gr1.CopyFromScreen(0, 0, 0, 0, fullScreen.Size);
                this.Opacity = 1;

                Magnifier.gr2.DrawImage(Magnifier.fullBmp, Magnifier.dstRect1, Magnifier.srcRect1, GraphicsUnit.Pixel);
                Magnifier.gr3.DrawImage(Magnifier.primaryBmp, Magnifier.dstRect2, Magnifier.srcRect2, GraphicsUnit.Pixel);
                Graphics gr = this.CreateGraphics();
                gr.DrawImage(Magnifier.newBmp, 0, 0);
                gr.Dispose();
                b1 = true;
            }
        }

       private static void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            return;
        }

    

        private void Magnifier_Shown(object sender, EventArgs e)
        {
            this.b1= true;
        }

        private void Magnifier_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (this.IsMoving2)
                {
                    Point pCursor = new Point(Cursor.Position.X, Cursor.Position.Y);
                    pCursorOffset.X = pCursor.X - pCursorLastCoordinate.X;
                    pCursorOffset.Y = pCursor.Y - pCursorLastCoordinate.Y;
                    this.Left = pthisLastCoordinate.X + pCursorOffset.X;
                    this.Top = pthisLastCoordinate.Y + pCursorOffset.Y;
                }
            }
        }

        private void Magnifier_MouseUp(object sender, MouseEventArgs e)
        {
            if (this.IsMoving2)
            {
                if (pCursorOffset.X == 0 && pCursorOffset.Y == 0)
                {
                    return;
                }
                if ((pthisLastCoordinate.X + pCursorOffset.X + this.Width) > 0)
                {
                    this.Left = pthisLastCoordinate.X + pCursorOffset.X;
                }
                else
                {
                    this.Left = 0;
                }
                if ((pthisLastCoordinate.Y + pCursorOffset.Y + this.Height) > 0)
                {
                    this.Top = pthisLastCoordinate.Y + pCursorOffset.Y;
                }
                else
                {
                    this.Top = 0;
                }

            }
          
            pCursorOffset.X = 0;
            pCursorOffset.Y = 0;
        }

       
    }
}
